//
//  MovielListViewController.m
//  豆瓣YHY
//
//  Created by dllo on 15/10/31.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "MovielListViewController.h"
#import "MBProgressHUD.h"
#import "MovieDetailCollectionViewController.h"
#import "ModelTableViewCell.h"
#import "Movie.h"
#import "AppTools.h"
#import "MovieDetailViewController.h"

#import "AFNetworking.h"
@interface MovielListViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, retain) MBProgressHUD *hub;
@property (nonatomic, copy) NSString *filepath;
@end

@implementation MovielListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor yellowColor];
    self.tableViewmovie = [[UITableView alloc]initWithFrame:[[UIScreen mainScreen]bounds] style:UITableViewStylePlain];
    [self.view addSubview:self.tableViewmovie];
    self.navigationItem.title = @"电影";
    

 
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"btn_nav_collection@2x.png"] style: UIBarButtonItemStylePlain target:self action:@selector(rightBarButtonItemAction:)];
    self.tableViewmovie.delegate = self;
    self.tableViewmovie.dataSource = self;
    
    [self.tableViewmovie registerClass:[ModelTableViewCell class] forCellReuseIdentifier:@"cell"];
    //self.myarraymovie = [[NSMutableArray alloc]init];
    self.myarraymovie = [NSMutableArray array];
    //    小菊花
    self.hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self.hub show:YES];
    self.hub.labelText = @"正在加载";
    NSLog(@"zzz");
    
    NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    self.filepath = [document stringByAppendingPathComponent:@"movie.xml"];
    //返回当
    NSLog(@"filePaht = %@", self.filepath);
    self.myarraymovie = [NSKeyedUnarchiver unarchiveObjectWithFile:self.filepath];
    
    
    [AppTools getLocalDataWithURL:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/movielist.php" andBlock:^(id dic) {
        
        self.myarraymovie = [NSMutableArray new];
        //self.myarraymovie = [[NSMutableArray alloc] init];
        NSMutableDictionary *dica = dic;
        NSMutableArray *array = [dica objectForKey:@"result"];
        NSLog(@"%@",array);
        
        for (NSMutableDictionary *dicb in array) {
            Movie *movie  = [[Movie alloc]init];
            [movie setValuesForKeysWithDictionary:dicb];//小字典往属性里传值
            
            [self.myarraymovie addObject:movie];
            
        }
        
        [NSKeyedArchiver archiveRootObject:self.myarraymovie toFile:self.filepath];
       
        [self.hub hide:YES]; // 小菊花消失
        [self.tableViewmovie reloadData];
    }];
    
}



    
   
    
















//九个
- (void)rightBarButtonItemAction:(UIBarButtonItem *)bartitle{
    MovieDetailCollectionViewController *moDCVC = [[MovieDetailCollectionViewController alloc]init];
    [self.navigationController pushViewController:moDCVC animated:YES];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.myarraymovie.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    

    static NSString *cell1 = @"cell";
    
    ModelTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cell1];
   Movie *movie  = [self.myarraymovie objectAtIndex:indexPath.row];
    cell.movieA = movie;
   // NSLog(@"%@",cell.movieA.movieNamep);
    return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 150;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MovieDetailViewController *movieDVC = [[MovieDetailViewController alloc]init];
    Movie *movie = [self.myarraymovie objectAtIndex:indexPath.row];
    movieDVC.movieb = movie;
    [self.navigationController pushViewController:movieDVC animated:YES];
    [movieDVC release];
}





























- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
